﻿using Microsoft.AspNetCore.Mvc.Formatters;
using MQTTnet.Protocol;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Net.Http.Json;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using Warehouse.IServices.TESinterface;
using Warehouse.Services.Repository;
using Warehouse.ViewModels.TESinterfaceDTO;
using WCS.Entities.BulkStorage;
using WCS.Entities;
using WCS.ViewModels.AGVDTO;
using WCS.Entities.TES;

namespace Warehouse.Services.TESinterface
{
    public class PsCarServices : IPsCarServices
    {
        protected readonly Repository<FeederSitesDTO> feeder;
        protected readonly Repository<SalverDTO> salver;
        protected readonly Repository<IanewayDTO> laneway;

        public PsCarServices(Repository<FeederSitesDTO> feeder, Repository<SalverDTO> salver, Repository<IanewayDTO> laneway)
        {
            this.feeder = feeder;
            this.salver = salver;
            this.laneway = laneway;
        }

        //申请接驳点
        public string preOccupyStation(FeederSitesDTO model)
        {
            var handler = new HttpClientHandler();
            handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls11;
            var httpclient = new HttpClient(handler);

            httpclient.DefaultRequestHeaders.Accept.Clear();
            httpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpContent httpContent = JsonContent.Create(model, typeof(FeederSitesDTO), MediaTypeHeaderValue.Parse("application/json"), new System.Text.Json.JsonSerializerOptions()
            {
                Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Default
            });

            HttpResponseMessage repsone = httpclient.PostAsync("http://10.6.0.3/tes/apiv2/preOccupyStation", httpContent).Result;
            if (repsone.IsSuccessStatusCode)
            {
                Task<string> t = repsone.Content.ReadAsStringAsync();
                if (t != null)
                {
                    return t.Result;
                }
            }
            return string.Empty;
            //string strurl = "http://47.94.193.70/tes/apiv2/preOccupyStation";
        }

        //托盘上架
        public string occupyStation(Salver model)
        {
            var handler = new HttpClientHandler();
            handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls11;
            var httpclient = new HttpClient(handler);

            httpclient.DefaultRequestHeaders.Accept.Clear();
            httpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpContent httpContent = JsonContent.Create(model, typeof(Salver), MediaTypeHeaderValue.Parse("application/json"), new System.Text.Json.JsonSerializerOptions()
            {
                Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Default
            });

            HttpResponseMessage repsone = httpclient.PostAsync("http://10.6.0.3/tes/apiv2/occupyStation", httpContent).Result;
            if (repsone.IsSuccessStatusCode)
            {
                Task<string> t = repsone.Content.ReadAsStringAsync();
                if (t != null)
                {
                    return t.Result;
                }
            }
            return string.Empty;
        }

        //巷道搬运(入库）
        public string newMovePodTask(IanewayDTO model)
        {
            var handler = new HttpClientHandler();
            handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls11;
            var httpclient = new HttpClient(handler);

            httpclient.DefaultRequestHeaders.Accept.Clear();
            httpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpContent httpContent = JsonContent.Create(model, typeof(IanewayDTO), MediaTypeHeaderValue.Parse("application/json"), new System.Text.Json.JsonSerializerOptions()
            {
                Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Default
            });

            HttpResponseMessage repsone = httpclient.PostAsync("http://10.6.0.3/tes/apiv2/newMovePodTask", httpContent).Result;
            if (repsone.IsSuccessStatusCode)
            {
                Task<string> t = repsone.Content.ReadAsStringAsync();
                if (t != null)
                {
                    return t.Result;
                }
            }
            return string.Empty;
        }

        //出库站点的搬运
        public string newMovePodTasks(DispatchHandlingDTO model)
        {
            var handler = new HttpClientHandler();
            handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls11;
            var httpclient = new HttpClient(handler);

            httpclient.DefaultRequestHeaders.Accept.Clear();
            httpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpContent httpContent = JsonContent.Create(model, typeof(DispatchHandlingDTO), MediaTypeHeaderValue.Parse("application/json"), new System.Text.Json.JsonSerializerOptions()
            {
                Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Default
            });

            HttpResponseMessage repsone = httpclient.PostAsync("http://10.6.0.3/tes/apiv2/newMovePodTask", httpContent).Result;
            if (repsone.IsSuccessStatusCode)
            {
                Task<string> t = repsone.Content.ReadAsStringAsync();
                if (t != null)
                {
                    return t.Result;
                }
            }
            return string.Empty;
        }

        //容器释放ContainerDTO
        public string releaseStation(ContainerDTO model)
        {
            var handler = new HttpClientHandler();
            handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls11;
            var httpclient = new HttpClient(handler);

            httpclient.DefaultRequestHeaders.Accept.Clear();
            httpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpContent httpContent = JsonContent.Create(model, typeof(ContainerDTO), MediaTypeHeaderValue.Parse("application/json"), new System.Text.Json.JsonSerializerOptions()
            {
                Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Default
            });

            HttpResponseMessage repsone = httpclient.PostAsync("http://10.6.0.3/tes/apiv2/releaseStation", httpContent).Result;
            if (repsone.IsSuccessStatusCode)
            {
                Task<string> t = repsone.Content.ReadAsStringAsync();
                if (t != null)
                {
                    return t.Result;
                }
            }
            return string.Empty;
        }

        //查询巷道列表
        public string getStorageLaneList(GetStorageLaneListDTO model)
        {
            var handler = new HttpClientHandler();
            handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls11;
            var httpclient = new HttpClient(handler);

            httpclient.DefaultRequestHeaders.Accept.Clear();
            httpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpContent httpContent = JsonContent.Create(model, typeof(GetStorageLaneListDTO), MediaTypeHeaderValue.Parse("application/json"), new System.Text.Json.JsonSerializerOptions()
            {
                Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Default
            });

            HttpResponseMessage repsone = httpclient.PostAsync("http://10.6.0.3/tes/apiv2/getStorageLaneList", httpContent).Result;
            if (repsone.IsSuccessStatusCode)
            {
                Task<string> t = repsone.Content.ReadAsStringAsync();
                if (t != null)
                {
                    return t.Result;
                }
            }
            return string.Empty;
        }
        //查询巷道
        public string getStorageLaneDetail(GetStorageLaneDetailDTO model)
        {

            var handler = new HttpClientHandler();
            handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls11;
            var httpclient = new HttpClient(handler);

            httpclient.DefaultRequestHeaders.Accept.Clear();
            httpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            HttpContent httpContent = JsonContent.Create(model, typeof(GetStorageLaneDetailDTO), MediaTypeHeaderValue.Parse("application/json"), new System.Text.Json.JsonSerializerOptions()
            {
                Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Default
            });

            HttpResponseMessage repsone = httpclient.PostAsync("http://10.6.0.3/tes/apiv2/getStorageLaneDetail", httpContent).Result;
            if (repsone.IsSuccessStatusCode)
            {
                Task<string> t = repsone.Content.ReadAsStringAsync();
                if (t != null)
                {
                    return t.Result;
                }
            }
            return string.Empty;
        }

        ////出库AGV
        //public string getStorageLaneDetailchu(AGVchuDTO model)
        //{

        //    var handler = new HttpClientHandler();
        //    handler.SslProtocols = System.Security.Authentication.SslProtocols.Tls11;
        //    var httpclient = new HttpClient(handler);

        //    httpclient.DefaultRequestHeaders.Accept.Clear();
        //    httpclient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        //    HttpContent httpContent = JsonContent.Create(model, typeof(BulkStorage), MediaTypeHeaderValue.Parse("application/json"), new System.Text.Json.JsonSerializerOptions()
        //    {
        //        Encoder = System.Text.Encodings.Web.JavaScriptEncoder.Default
        //    });

        //    HttpResponseMessage repsone = httpclient.PostAsync("http://10.6.3.1:9099/api/Job/PortCreateTask", httpContent).Result;
        //    if (repsone.IsSuccessStatusCode)
        //    {
        //        Task<string> t = repsone.Content.ReadAsStringAsync();
        //        if (t != null)
        //        {
        //            return t.Result;
        //        }
        //    }
        //    return string.Empty;
        //}
    }
}
